package com.cslg.ac.demo.service.user;


import com.cslg.ac.demo.constant.ResponseCode;
import com.cslg.ac.demo.dao.jedis.JedisDAO;
import com.cslg.ac.demo.dao.user.person.PersonInfoDAO;
import com.cslg.ac.demo.dao.user.team.TeamHeadDAO;
import com.cslg.ac.demo.dao.user.team.TeamInfoDAO;
import com.cslg.ac.demo.dao.user.UserAuthDAO;
import com.cslg.ac.demo.dao.user.UserDAO;
import com.cslg.ac.demo.dao.user.po.person.PersonInfoPO;
import com.cslg.ac.demo.dao.user.po.UserAuthPO;
import com.cslg.ac.demo.dao.user.po.UserPO;
import com.cslg.ac.demo.dao.user.team.TeamProfileDAO;
import com.cslg.ac.demo.entity.user.User;
import com.cslg.ac.demo.util.string.EncryptionUtil;
import com.cslg.ac.demo.util.string.SaltUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@PropertySource(value = {"classpath:user.properties"},encoding="utf-8")
public class UserService {
    @Autowired
    JedisDAO jedisDAO;
    @Autowired
    PersonInfoDAO personInfoDAO;
    @Autowired
    TeamInfoDAO teamInfoDAO;
    @Autowired
    UserAuthDAO userAuthDAO;
    @Autowired
    UserDAO userDAO;
    @Autowired
    TeamHeadDAO teamHeadDAO;
    @Autowired
    TeamProfileDAO teamProfileDAO;
    @Value("${teamType}")
    int teamType;
    @Value("${personType}")
    int personType;
    @Value("${personBuket}")
    String personBuket;




    public Integer getId(String phone){
        Integer id = userDAO.selectIdByPhone(phone);
        return id;
    }

    public int login(User user){
        String name = user.getUserName();
        UserPO userPO = userDAO.SelectByName(name);
        if(userPO == null){
            return ResponseCode.USER_NOT_EXISTS;
        }
        if(!EncryptionUtil.auth(user.getPassword()+userPO.getSalt(),userPO.getPassword())){
            return ResponseCode.PASSWORD_ERROR;
        }
        if(user.getUserType() != userPO.getUserType()){
            return ResponseCode.PARAM_ILLEGAL;
        }

        user.setId(userPO.getId());
        user.setUserType(userPO.getUserType());
        return ResponseCode.REQUEST_SUCCESSED;
    }

    public int register(User user){
        UserPO userPO = new UserPO();
        userPO.setUserName(user.getUserName());
        UserAuthPO userAuthPO = userAuthDAO.selectByName(user.getUserName());
        if(userAuthPO == null || userAuthPO.getPhoneAuth() == 1){
            return ResponseCode.USER_NOT_AUTH;
        }
        if(userDAO.SelectByName(user.getUserName()) != null){
            return ResponseCode.USER_ALREADY_EXISTS;
        }
        String salt = SaltUtil.getSalt();
        byte[] password = EncryptionUtil.getEncryptedBytes(user.getPassword()+salt);
        userPO.setId(userAuthPO.getId());
        userPO.setPassword(password);
        userPO.setUserType(user.getUserType());
        userPO.setSalt(salt);
        userDAO.Insert(userPO);
        if(user.getUserType() == teamType){
           int id = userAuthPO.getId();
           teamHeadDAO.insert(id,user.getUserName());
           teamInfoDAO.insert(id);
           teamProfileDAO.insert(id);
        }
        if(user.getUserType() == personType){
            PersonInfoPO personInfoPO = new PersonInfoPO();
            personInfoPO.setId(userAuthPO.getId());
            personInfoPO.setPhone(userAuthPO.getUserName());
            personInfoDAO.insert(personInfoPO);
        }
        return ResponseCode.REQUEST_SUCCESSED;
    }
}
